import { SettingsModel } from 'server/models/Settings'
import { successResponse, errorResponse } from 'server/utils/response'

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event)
    
    // 验证必填字段
    if (!body) {
      return errorResponse('请求体不能为空')
    }

    // 批量更新统计代码设置
    const success = await SettingsModel.batchUpdate(body, 'analytics')
    
    if (!success) {
      throw new Error('保存统计代码信息失败')
    }
    
    return successResponse(null, '统计代码信息保存成功')
  } catch (error) {
    console.error('保存统计代码信息失败:', error)
    
    return errorResponse(
      '保存统计代码信息失败',
      error instanceof Error ? error.message : '未知错误'
    )
  }
})